package com.tangtanwei.hotel.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangtanwei.hotel.entity.Room;
import com.tangtanwei.hotel.entity.dto.RoomQueryDto;
import com.tangtanwei.hotel.entity.enums.RoomStatusEnums;
import com.tangtanwei.hotel.mapper.RoomMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoomService extends ServiceImpl<RoomMapper, Room> {


    public String saveOrupdate(Room room) {
        if (room.getId() == null){
            if (this.count(new QueryWrapper<Room>().eq("name",room.getName())) > 0)
                return "该房间名称已经存在，不能重复创建";

            room.setState(RoomStatusEnums.FREE.value);
            room.setCount(room.getTotalCount());
        }

        this.saveOrUpdate(room);
        return "保存成功";
    }

    public List<Room> search(String keyword) {
        return this.list(new QueryWrapper<Room>().like("name",keyword).or().like("state",keyword)
                .or().like("price",keyword).or().like("introduction",keyword));
    }


    public List<Room> getByCondition(RoomQueryDto roomQueryDto) {
        return baseMapper.getByCondition(roomQueryDto);
    }
}
